const db = require("../db/index");

/**
 * 发布留言评论
 * @param { wall_id,message } req
 * @param {*} res
 */

exports.addWallComments = (req, res) => {
  const sql = `insert into wall_comment set ? `;
  const sql2 = `update wall set comment_num = comment_num+1 where id = ?`;
  const commentData = {
    ...req.body,
    user_id: req.user.id,
    moment: new Date(),
  };
  db.query(sql, commentData, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("发布评论失败！");
    db.query(sql2, req.body.wall_id, (err, result) => {
      res.cc("发布评论成功", 0);
    });
  });
};

/**
 *  获取对应卡片评论信息
 * @param {wall_id } req
 * @param {username ,message,user_pic ,moment} res
 */

exports.getWallComments = (req, res) => {
  const sql = `SELECT wall_comment.message, wall_comment.moment, users.username, users.user_pic 
  FROM wall_comment
  INNER JOIN users ON wall_comment.user_id = users.id
  WHERE wall_comment.is_delete != 1 AND wall_comment.wall_id = ?  order by wall_comment.moment desc`;

  db.query(sql, req.query.wall_id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      message: "获取成功",
      data: results,
      status: 0,
    });
  });
};
